package com.achievo.know.domain;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import com.dayatang.domain.BaseEntity;
import com.dayatang.domain.InstanceFactory;


/**
 * @author hp
 *
 */
/**
 * @author hp
 *
 */
@Entity
@Table(name="RAMW_NEWS")
public class News extends BaseEntity{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3576472460907567341L;

	//浏览量
	@Column(name="BROWSER_SUM")
	private int browserNum;
	
	//评论量
	@Column(name="FAQ_SUM")
	private int faqSum;
	
	//是否可评论
	@Column(name="CAN_FAQ")
	private boolean canFaq;
	@Transient
	private String canFaqStr;
	//是否可下载
	@Column(name="CAN_DOWN")
	private boolean canDown;
	//是否可下载
	@Transient
	private String canDownStr;
	
	//分类
	@Column(name="CATEGORY",length=50)
	private String category;
	
	@Column(name="CATEGORY_ID")
	private Long categoryId;
	
	//备注
	@Column(name="DESCRIPTION",length=2000)
	private String desc;
	
	//关键字
	@Column(name="KEYWORD",length=200)
	private String keyword;
	
	//标题
	@NotNull
	@Column(name="TITLE",length=200,nullable=false)
	private String title;
	
	//发布时间
	@NotNull
	@Column(name="PUBLISH_TIME",nullable=false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date publishTime;
	
	@Column(name="EDIT_TIME")
	@Temporal(TemporalType.TIMESTAMP)
	private Date editTime;
	
	//推荐时间
	@Column(name="SUGEST_TIME")
	@Temporal(TemporalType.TIMESTAMP)
	private Date sugestTime;
	
	//发布人
	@NotNull
	@Column(name="PUBLISH_USER",length=20,nullable=false)
	private String publishUser;
	
	@Column(name="PUBLISH_USER_NAME",length=20)
	private String publishUserName;
	
	@Column(name="EDIT_USER",length=20)
	private String editUser;
	
	//内容
	@OneToMany(mappedBy = "news", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	private Set<NewsContent> content;
	
	//附件
	@Transient
	private List<FileAttachment> attachments;
	
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name = "RAMS_NEWS_R_AREA", joinColumns = { @JoinColumn(name = "NEWS_ID",insertable=false) }, inverseJoinColumns = { @JoinColumn(name = "AREA_ID",insertable=true) })
	private Set<Area> areas = new HashSet<Area>();
	
	//图片
	@Transient
	private FileAttachment newsImage;
	
	@Column(name="NEWS_POSITION")
	private int position;
	
	@Column(name="ATTACH_POSITION")
	private Integer attachPosition;
	
	
	public String getPublishUserName() {
		return publishUserName;
	}

	public void setPublishUserName(String publishUserName) {
		this.publishUserName = publishUserName;
	}

	public Date getEditTime() {
		return editTime;
	}

	public void setEditTime(Date editTime) {
		this.editTime = editTime;
	}

	public String getEditUser() {
		return editUser;
	}

	public void setEditUser(String editUser) {
		this.editUser = editUser;
	}

	public Date getSugestTime() {
		return sugestTime;
	}

	public void setSugestTime(Date sugestTime) {
		this.sugestTime = sugestTime;
	}

	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public FileAttachment getNewsImage() {
		return newsImage;
	}

	public void setNewsImage(FileAttachment newsImage) {
		this.newsImage = newsImage;
	}

	public Set<Area> getAreas() {
		return areas;
	}

	public void setAreas(Set<Area> areas) {
		this.areas = areas;
	}




	@Transient
	private List<Faq> faqs;
	
	
	

	public List<Faq> getFaqs() {
		return faqs;
	}

	public void setFaqs(List<Faq> faqs) {
		this.faqs = faqs;
	}

	public int getBrowserNum() {
		return browserNum;
	}

	public void setBrowserNum(int browserNum) {
		this.browserNum = browserNum;
	}

	public int getFaqSum() {
		return faqSum;
	}

	public void setFaqSum(int faqSum) {
		this.faqSum = faqSum;
	}

	public boolean isCanFaq() {
		return canFaq;
	}

	public void setCanFaq(boolean canFaq) {
		this.canFaq = canFaq;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Date getPublishTime() {
		return publishTime;
	}

	public void setPublishTime(Date publishTime) {
		this.publishTime = publishTime;
	}

	public String getPublishUser() {
		return publishUser;
	}

	public void setPublishUser(String publishUser) {
		this.publishUser = publishUser;
	}
	
	public Set<NewsContent> getContent() {
		return content;
	}

	public void setContent(Set<NewsContent> content) {
		this.content = content;
	}

	public List<FileAttachment> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<FileAttachment> attachments) {
		this.attachments = attachments;
	}

	@Override
	public boolean equals(Object arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
	
	
	public String getCanFaqStr() {
		return canFaqStr;
	}

	public void setCanFaqStr(String canFaqStr) {
		this.canFaqStr = canFaqStr;
	}




	private static NewsRepository newsRepository;
	
	public static NewsRepository newsRepository() {
		if (newsRepository == null) {
			newsRepository = InstanceFactory.getInstance(NewsRepository.class);
		}
		return newsRepository;
	}
	
	public NewsContent getNewsContent(){
		Set<NewsContent> ns=this.getContent();
		Iterator<NewsContent> i= ns.iterator();
		while(i.hasNext()){
			return i.next();
		}
		return null;
	}

	public void addBrowserSum() {
		this.setBrowserNum(this.getBrowserNum()+1);
		newsRepository().save(this);
	}

	public boolean isCanDown() {
		return canDown;
	}

	public void setCanDown(boolean canDown) {
		this.canDown = canDown;
	}

	public String getCanDownStr() {
		return canDownStr;
	}

	public void setCanDownStr(String canDownStr) {
		this.canDownStr = canDownStr;
	}

	public Integer getAttachPosition() {
		return attachPosition;
	}

	public void setAttachPosition(Integer attachPosition) {
		this.attachPosition = attachPosition;
	}
	
	//是否被收藏
	public boolean isAddFavorite(String username){
		return newsRepository().isAddFavorite(this.getId(),username);
	}
	
	@Transient
	private Date addFavTime;
	
	
	public Date getAddFavTime() {
		return addFavTime;
	}

	public void setAddFavTime(Date addFavTime) {
		this.addFavTime = addFavTime;
	}

	public News() {
		super();
	}
	public News(Long id,String title, String category, Date addFavTime) {
		super();
		this.id=id;
		this.title = title;
		this.category = category;
		this.addFavTime = addFavTime;
	}
}
